<template>
  <div>
    <el-dialog :title="title" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="confirmFn">立即创建</el-button>
          <el-button @click="closeFn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addRoles, editRoles, getRolesId } from '@/api/roles'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '',
      formData: {
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 1, max: 250, message: '长度在 1 到 250 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getRolesId(id) {
      const res = await getRolesId(id)
      this.formData = res.data
    },
    async confirmFn() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editRoles(this.formData)
      } else {
        await addRoles(this.formData)
      }
      this.closeFn()
      this.$emit('updateEvent')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formData = {
        title: '',
        description: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

  <style lang="scss" scoped>

  </style>
